<?php
namespace Admin\Controller;
use Model\GoodsModel;
//use Think\Controller;
use Tools\AdminController;
use Think\Model;
class GoodsController extends AdminController {
    // 商品列表
    public function zhanshi1(){
//        $goods = new GoodsModel();

        //1.实例化普通model对象
//        $goods = new \Model\GoodsModel();//命名空间方式实例化对象
//        var_dump($goods);

        //2.实例化父类Model对象
//        $model = D();  //new Model()；
//        var_dump($model);//可以用于执行原声SQL语句

        //实例化父类Model对象，同时操作数据表
//        $user = D('Goods');//实例化父类Model对象，同时操作sw_goods数据表
//        var_dump($user);
        //该方式使得一个数据表即使没有对应的Model模型类，也不影响我们直接操作该表

    	$this->display();
    }
    public function  zhanshi2(){
        $goods = new \Model\GoodsModel();
        //1.查询全部记录.全部字段
        $info = $goods->select();
        //2.根据主键id值查询记录
        $info = $goods->select(9);
        //3.主键id值在某个范围的记录
        $info = $goods->select("20,23,25,29");

//        dump($info);
        //把获得的信息传递给模板使用
        $this->assign('info',$info);

        $this->display();

    }
    public function  zhanshi3(){
        $goods = D('Goods');
        //数据操作常用方法
        //1.where() 限制操作(查询)条件
        //$goods ->where('goods_price>1000 and  goods_name like "诺%"');//sql语句where后边的信息都可以作为where方法的参数
        //$info = $goods->select();

        //2.field() 限制查询的字段
//        $goods->field('goods_id,goods_name');
//        $info = $goods->select();

        //3.limit(长度); 限制查询的条数
        //limit(偏移量,长度); 偏移量=（页码-1）*长度
        //$goods->limit(5);
        //$info = $goods->select();

        //4.order()排序查询 desc 降序，asc 默认升
        $goods->order('goods_id desc');
        $info = $goods->select();

        //5.group() 分组查询
//        $goods->group('goods_brand_id');
//        $goods->field('goods_brand_id,max(goods_price)');
//        dump($info);

        //6.having() 条件设置方法与where类似,条件必须是结果集存在的字段
//        $goods ->having('goods_price>1000');
//        $info = $goods->select();

        $this->assign('info',$info);
        $this->display();
    }
    public function  zhanshi(){

        $goods = D('Goods');


        //方法的连贯操作，一个条件一个条件加
//        $info=$goods->field('goods_name')->where('goods_price>1000')->limit(10)->order('goods_price desc')->select();
//        $goods->order('goods_id desc');
//        $info = $goods->select();

//        $count = $goods->count();//获取所有数据可以中间加where控制条件
//        $page = new \Think\Page($count,10);//条数和每页个数
//        $show = $page->show();
//        $info = $goods->order('goods_id desc')->limit($page->firstRow.','.$page->listRows)->select();
//        $this->assign('page',$show);// 赋值分页输出

        $total = $goods -> count();
        $per = 6;//每页显示数据行数
        $page_obj = new \Tools\Page($total,$per);
        $sql = "select * from sw_goods order by goods_id desc ".$page_obj->limit;
        $info = $goods ->query($sql);

        $pagelist = $page_obj->fpage(array(3,4,5,6,7,8));
        $this->assign('pagelist',$pagelist);

        $this->assign('info',$info);// 赋值数据集
        $this->display();//展示
    }
    //添加商品
    public function tianjia1(){
        $goods = new \Model\GoodsModel();
        //数据添加
        //1.数组方式
//        $arr = array(
//            'goods_name' => 'samsung6s',
//            'goods_price'=>'5600',
//            'goods_weight' =>130,
//            'goods_number' =>16,
//        );
//        $z = $goods ->add($arr);
//        dump($z);//返回记录主键id
        //2.AR方式
        //对象给本身不存在的属性进行赋值(包括给私有属性)，要执行魔术方法_set
        $goods ->goods_name = "nokia345";
        $goods ->goods_price = 2100;
        $goods ->goods_weight = 90;
        $z = $goods->add();
//        dump($z);

        $this->display();
    }
    public function tianjia(){
        $goods = D('Goods');
        //两个逻辑：展示表单，收集表单
        if(!empty($_POST)){
            //处理上传的图片附件
            if($_FILES['goods_pic']['error']<4){
                //A。处理上传的图片附件
                $cfg = array(
                    'rootPath'      =>  './Public/Uploadss/', //保存根路径
                );
                $up = new \Think\Upload($cfg);
                $z = $up ->uploadOne($_FILES['goods_pic']);
                $bigimg = $up ->rootPath.$z['savepath'].$z['savename'];//大图存放位置
                $smallimg = $up ->rootPath.$z['savepath'].'small_'.$z['savename'];//大图存放位置

                //B。对上传好的图片制作说略图
                $im = new \Think\Image();//实例化Image对象
                $im ->open($bigimg);
                $im ->thumb(100,100);//制作缩略图，默认等比例缩放效果
                $im ->save($smallimg);

                //ltrim($bigimg,'./');去掉路径前的./
                $_POST['goods_big_img'] = $bigimg;
                $_POST['goods_small_img'] = $smallimg;
//                dump($up->getError());
//                dump($z);
                //把上传的附件存储入数据库
            }

            //收集表单，并存入数据库
            $shuju = $goods ->create();
            $z = $goods->add($shuju);
            if($z){
//                $this->redirect([分组/控制器/操作方法]地址，参数，延迟时间，提示信息);
                $this->redirect('zhanshi',array('name'=>'tom','age'=>22),1,'添加商品成功');
            }else{
                $this->redirect('tianjia',array('name'=>'tom','age'=>22),1,'添加商品失败');
            }
        }else{
            //展示表单
            $this->display();
        }
    }
    //修改商品
    public function xiugai1(){
        $goods = new \Model\GoodsModel();
        //数据修改
        //1.数组方式
//        $arr = array(
//            $goods ->goods_id = 58;
//            'goods_name' => 'huawei111',
//            'goods_price'=>'1700',
//            'goods_number' =>16,
//        );
//        $z = $goods ->save($arr);
//        dump($z);//返回记录主键id
        //2.AR方式
        //对象给本身不存在的属性进行赋值(包括给私有属性)，要执行魔术方法_set
        //数据修改二选一:主键id或者where()方法
        //$goods ->goods_id = 58;
        $goods ->goods_name = "nokia2500";
        $goods ->goods_price = 1200;
        $goods ->goods_weight = 90;
//        $z = $goods->save();
        $z = $goods->where('goods_id>56')->save();
//        dump($z);
        $this->display();
    }
    public function xiugai($goods_id){
        //$goods_id,展示表传递过来的

        $goods = D('Goods');
        //两个逻辑：展示，收集
        if(!empty($_POST)){
            //收集表单，并存入数据库
            $z = $goods->save($_POST);
            if($z){
//                $this->redirect([分组/控制器/操作方法]地址，参数，延迟时间，提示信息);
                $this->redirect('zhanshi',array(),3,'修改商品成功');
            }else{
                $this->redirect('xiugai',array('goods_id'=>$goods_id),3,'修改商品失败');
            }
        }else{
            //根据$goods_id获得被修改的商品信息，并给模板
            //find()方法只负责给返回一条记录结果，并且是一维数组返回
            $info = $goods->find($goods_id);

            //展示表单
            $this ->assign('info',$info);
            $this->display();
        }
    }

    //删除商品
    public function shanchu($goods_id){
        $goods = D('Goods');
        $z = $goods->delete($goods_id);
        if($z){
            $this->redirect('zhanshi',array(),3,'删除商品成功');
        }else{
            $this->redirect('zhanshi',array('goods_id'=>$goods_id),1,'删除商品失败');
        }
    }
}